#include "newcube.h"
#include "ui_newcube.h"
#include "QPainter"
#include "QMouseEvent"
#include "QKeyEvent"
#include "QMessageBox"


newcube::newcube(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::newcube),start(false)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,100,1100,1200));
    ui->label_3->setAttribute(Qt::WA_TransparentForMouseEvents, true);
}

newcube::~newcube()
{
    delete ui;
}
void newcube::paintEvent(QPaintEvent *event)
{
    QPainter painters(this);
    //绘制背景
    painters.setPen(Qt::darkBlue);//画笔颜色
    painters.setBrush(Qt::black);//填充颜色
    painters.drawRect(10,10,1080,1180);
    painters.drawPixmap(10,10,1080,1180,QPixmap(":/images/pictures/picture.jpg"));
//绘制格子
painters.setPen(Qt::gray);//画笔颜色
painters.setBrush(Qt::gray);//填充颜色
painters.drawRect(15,15,570,1170);
painters.setPen(Qt::black);
painters.drawRect(595,15,490,600);
painters.drawRect(595,600,490,550);
painters.drawPixmap(15,15,570,1170,QPixmap(":/images/pictures/picture4.jpeg"));
painters.drawPixmap(595,15,490,600,QPixmap(":/images/pictures/picture3.jpg"));
painters.drawPixmap(595,600,490,570,QPixmap(":/images/pictures/picture3.jpg"));
painters.setPen(Qt::gray);
for(int i=1;i<48;i++)
{
    painters.drawLine(25,i*25,575,i*25);
}
for(int i=1;i<24;i++)
{
    painters.drawLine(i*25,25,i*25,1175);
}
//显示游戏开始和结束
QFont font1("Microsoft YaHei",20);
painters.setFont(font1);
painters.setPen(Qt::darkGray);
painters.setBrush(Qt::white);
painters.drawText(630,100,"请选择您想要保留的方块");
painters.drawText(630,400,QString::number(clean1));

//绘制块块
painters.setPen(Qt::black);
painters.setBrush(Qt::black);
painters.drawRect(700,300,300,200);
painters.drawPixmap(700,300,300,200,QPixmap(":/images/pictures/picture3.jpg"));
painters.setPen(Qt::gray);//画笔颜色
painters.setBrush(Qt::cyan);//填充颜色
if(!start)
    vCube.resize(0);
start = true;
if(vCube.size()>0)
    painters.drawRects(&vCube[0],vCube.size());
//painters.drawRects(&vCubeRect[0],vCubeRect.size());
for(int i=0;i<vCube.size();i++)
    painters.drawPixmap(vCube[i],QPixmap(":/images/pictures/picture16.jpg"));
}

void newcube::mousePressEvent(QMouseEvent *event)
{
    p1 = event->pos();
    drawcube(p1);
}
void newcube::mouseMoveEvent(QMouseEvent *event)
{
    p2 = event->pos();
    drawcube(p2);
}
void newcube::drawcube(QPoint p)
{
    if(p.x()/25*25<=550&&p.y()/25*25<=1150&&p.x()/25*25>=25&&p.y()/25*25>=25)
    {
        int relevant = 0;
        QRect a(p.x()/25*25,p.y()/25*25,25,25);
        for(int i=0;i<vCube.size();i++)
        {
            if(vCube[i]==a)
            {
                relevant = 1;
                break;
            }
        }
        if(relevant==0)
        {
            vCube.resize(vCube.size()+1);
            vCube[vCube.size()-1] = a;
        }

        update();
    }
}

void newcube::clean(int a)
{
    QVector<QRect>::iterator ite;
    for(ite=vCube.begin();ite!=vCube.end();)
    {
          if((*ite).top()<a*11.75)
                ite=vCube.erase(ite);
          else
                ite++;
    }
}

void newcube::on_dial_valueChanged(int value)
{
    clean(value);
    clean1 = value;
    update();
}

void newcube::on_toolButton_clicked()
{
    d.vCube2.resize(vCube.size());
    for(int i = 0;i<vCube.size();i++)
    {
        d.vCube2[i] = vCube[i];
    }

    d.from = 1;
    this->close();
    d.show();
    d.setWindowTitle("俄罗斯方块");
}

void newcube::on_toolButton_2_clicked()
{
    QMessageBox::information(NULL,"游戏介绍","用鼠标点击并滑动创建可以消去的障碍物，完成后点击start开始游戏");
}
